#include "searchtab.h"
#include "searchtop.h"

#include <WhaleButton>
#include <WhaleTweetEntry>

#include <QScrollArea>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QShortcut>
#include <QLabel>

SearchTab::SearchTab( QString query, QWidget * parent ) : WhaleStreamTab( parent ) {
    SearchTop * top = new SearchTop();
    this->mSearchBox = top->searchBox();
    this->mSearchBox->setText( query );

    this->setTitle( top );

    this->mConnector = new WhaleTwitterConnect();
    this->addConnector( this->mConnector ); // parent

    connect( this->mSearchBox, SIGNAL( returnPressed() ), this, SLOT( search() ) );
    connect( top->searchLabel(), SIGNAL( clicked() ), this, SLOT( search() ) );
    connect( this, SIGNAL( newStreamData() ), this, SLOT( clearStream() ) );

    if ( !query.isEmpty() ) {
        this->search();
    }
}

void SearchTab::search() {
    if ( this->mSearchBox->text().isEmpty() ) {
        return;
    }
    this->clearStream();
    this->mLastQuery = this->mSearchBox->text();
    this->streamLayout()->addWidget( new QLabel( "Loading " + this->mLastQuery + "..." ) );
    this->mConnector->search( this->mLastQuery );
}

QLineEdit * SearchTab::searchBox() {
    return this->mSearchBox;
}

QString SearchTab::lastQuery() {
    return this->mLastQuery;
}

WhaleTweetEntry * SearchTab::newEntry( QVariantMap data, QString streamName ) {
    return new WhaleTweetEntry( data );
}
